import {
    reqBanner,
    reqGetnew,
    reqGroupon,
    reqCountdown,
    reqSeckill,
    reqIndexGoods,
} from "../../request/api";

import { createSelector } from "reselect"

//初始值
const initState = {
    banner: [],
    getNew: [],
    groupon:[],
    countdown:[],
    seckill:[],
    indexGoods:[],
    flagGoods:0,
}

//actionTypes
const types = {
    CHANGE_BANNER: "HOME_CHANGE_BANNER",
    CHANGE_GET_NEW: "HOME_CHANGE_GET_NEW",
    CHANGE_GROUPON:"HOME_CHANGE_GROUPON",
    CHANGE_COUNTDOWN:"HOME_CHANGE_COUNTDOWN",
    CHANGE_SECKILL:"HOME_CHANGE_SECKILL",
    CHANGE_INDEX_GOODS:"HOME_CHANGE_INDEX_GOODS",
    CHANGE_FLAG_GOODS:"HOME_CHANGE_FLAG_GOODS",
}

//reducer 修改数据
const reducer = (state = initState, action) => {
    switch (action.type) {
        case types.CHANGE_BANNER:
            return {
                ...state,
                banner: action.arr
            }
            case types.CHANGE_GET_NEW:
                return {
                    ...state,
                    getNew: action.arr
                }
            case types.CHANGE_GROUPON:
                return {
                    ...state,
                    groupon:action.arr
                }
            case types.CHANGE_COUNTDOWN:
                return {
                    ...state,
                    countdown:action.arr
                }
            case types.CHANGE_SECKILL:
                return {
                    ...state,
                    seckill:action.arr
                }
            case types.CHANGE_INDEX_GOODS:
                return {
                    ...state,
                    indexGoods:action.arr
                }
            case types.CHANGE_FLAG_GOODS:
                return {
                    ...state,
                    flagGoods:action.num
                }
            default:
                return state;
    }
}

//actionCreators组件派发的动作
export const actions = {
    changeBanner: (arr) => ({type: types.CHANGE_BANNER,arr}),
    changeGetNew: (arr) => ({type: types.CHANGE_GET_NEW,arr}),
    changeGroupon: (arr) => ({type: types.CHANGE_GROUPON,arr}),
    changeCountdown: (arr) => ({type: types.CHANGE_COUNTDOWN,arr}),
    changeSeckill: (arr) => ({type: types.CHANGE_SECKILL,arr}),
    changeIndexGoods: (arr) => ({type: types.CHANGE_INDEX_GOODS,arr}),
    changeFlagGoods: (num) => ({type: types.CHANGE_FLAG_GOODS,num}),

    reqBanner: () => (dispatch, getState) => {
        reqBanner().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeBanner(res.data.list))
            }
        })
    },
    getGetNew: () => (dispatch, getState) => {
        reqGetnew().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeGetNew(res.data.list))
            }
        })
    },
    reqGroupon: () => (dispatch, getState) => {
        reqGroupon().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeGroupon(res.data.list))
            }
        })
    },
    reqCountdown: () => (dispatch, getState) => {
        reqCountdown().then(res => {
            if (res.data.code === 200) {
                // dispatch(actions.changeCountdown(res.data.list))
            }
        })
    },
    reqSeckill: () => (dispatch, getState) => {
        reqSeckill().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeSeckill(res.data.list))
            }
        })
    },
    reqIndexGoods: () => (dispatch, getState) => {
        reqIndexGoods().then(res => {
            if (res.data.code === 200) {
                dispatch(actions.changeIndexGoods(res.data.list))
            }
        })
    },
}

//selectors导出数据
export const getBanner = (state) => state.home.banner
export const getGetNew = (state) => state.home.getNew
export const getGroupon = (state) => state.home.groupon
export const getCountdown = (state) => state.home.countdown
export const getSeckill = (state) => state.home.seckill
export const getIndexGoods = (state) => state.home.indexGoods
export const getFlagGoods = (state) => state.home.flagGoods

export const getShowGoods = createSelector(
    [getIndexGoods, getFlagGoods],
    (indexGoods, flagGoods) => {
        console.log("重新计算展示的订单");
        return indexGoods[flagGoods]
    }
)


export default reducer;